// OpenCallbackGUI.cpp

#include "StdAfx.h"

#include "OpenCallbackGUI.h"

#include "Common/StdOutStream.h"
#include "Common/StdInStream.h"
#include "Common/StringConvert.h"
#include "Common/MiscUtilities.h"

#ifndef _NO_CRYPTO
#include "../FileManager/PasswordDialog.h"
#endif

#include "../KodService/kod_api.h"

HRESULT COpenCallbackGUI::CheckBreak()
{
  return S_OK;
}

HRESULT COpenCallbackGUI::SetTotal(const UInt64 * /* files */, const UInt64 * /* bytes */)
{
  return S_OK;
}

HRESULT COpenCallbackGUI::SetCompleted(const UInt64 * /* files */, const UInt64 * /* bytes */)
{
  return S_OK;
}
 
#ifndef _NO_CRYPTO
HRESULT COpenCallbackGUI::CryptoGetTextPassword(BSTR *password)
{
  //<8zip>
  if(*password)
  {
#if (!defined(_SFX)) && (!defined(_SFXWIN32))
    // reused for 8z property
    UString ustr1=*password;
    UString ustr2=kGetPassword8z;
    if(ustr1.Find(ustr2)>=0)
    {
      if(!PasswordIsDefined)
      {
        CMyProperty prop;
        UString prop_str;
        prop_str=ustr1.Mid(ustr2.Length(), ustr1.Length()-ustr2.Length());
        HexUStringToBytes((char *)&prop, prop_str, prop_str.Length()); 

        // get 8zip password here...
        if(prop.key_no>0)
        {
          UString pwd;
          HRESULT rel=DecodeMyPassword(pwd, prop, ParentWindow);
          if(rel==S_OK)
          {
            PasswordWasAsked = true;
            PasswordIsDefined = true;
            Password = pwd;
          }
          else
            return E_ABORT;
        }
        else
        {
          CPasswordDialog dialog;
          if (dialog.Create(ParentWindow) == IDCANCEL)
            return E_ABORT;
          PasswordWasAsked = true;
          PasswordIsDefined = true;
          Password = GetMyPasswordHash(dialog.Password);
        }
      }
    }
#else
    return S_FALSE;
#endif
  }
  //</8zip>

  PasswordWasAsked = true;
  if (!PasswordIsDefined)
  {
    CPasswordDialog dialog;
    if (dialog.Create(ParentWindow) == IDCANCEL)
      return E_ABORT;
    Password = dialog.Password;
    PasswordIsDefined = true;
  }
  CMyComBSTR tempName(Password);
  *password = tempName.Detach();
  return S_OK;
}

HRESULT COpenCallbackGUI::GetPasswordIfAny(UString &password)
{
  if (PasswordIsDefined)
    password = Password;
  return S_OK;
}

bool COpenCallbackGUI::WasPasswordAsked()
{
  return PasswordWasAsked;
}

void COpenCallbackGUI::ClearPasswordWasAskedFlag()
{
  PasswordWasAsked = false;
}

#endif  

